package com.villaday.idf.adapter3;

import android.content.Context;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.villaday.idf.R;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.custom.foldtext.FoldTextView;
import com.villaday.idf.model.MaterialRecommendEntity;
import com.villaday.idf.model.RefreshtokenEntity;
import com.villaday.idf.util.LogUtil;
import com.villaday.idf.util.SharedPreferencesUtil;
import com.yarolegovich.discretescrollview.DSVOrientation;
import com.yarolegovich.discretescrollview.DiscreteScrollView;
import com.yarolegovich.discretescrollview.InfiniteScrollAdapter;
import com.yarolegovich.discretescrollview.transform.ScaleAndAlphaTransformer;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import byc.imagewatcher.MessagePicturesLayoutScro;

/**
 * 首页动态列表
 */

public class VipDynamicListAdapter extends AbsRecyclerViewAdapter<MaterialRecommendEntity.BodyEntity.DataListEntity> {
    private OnAdapterCallbackListener onAdapterCallbackListener;
    private View.OnClickListener onClickListener;
    private Callback callback;
    private MessagePicturesLayoutScro.Callback mCallback;
    private final String roleType;
    private final SimpleDateFormat format;
    private final SimpleDateFormat formatNew;
    private final RefreshtokenEntity.BodyBean roleInfo;

    public VipDynamicListAdapter(Context context, OnAdapterCallbackListener onAdapterCallbackListener, View.OnClickListener onClickListener, Callback callback) {
        super(context, R.layout.item_dynamic_home0_vip, /*R.layout.item_dynamic_1, R.layout.item_dynamic_2,*/ R.layout.item_page_loading,
                R.layout.item_page_bottom);
        this.onAdapterCallbackListener = onAdapterCallbackListener;
        this.onClickListener = onClickListener;
        this.callback = callback;

        roleInfo = SharedPreferencesUtil.getRoleInfo(context);
        roleType = roleInfo.getMember().getType();

        format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatNew = new SimpleDateFormat("yyyy/MM/dd");
    }

    public VipDynamicListAdapter setPictureClickCallback(MessagePicturesLayoutScro.Callback callback) {
        mCallback = callback;
        return this;
    }

    @Override
    public int getItemType(MaterialRecommendEntity.BodyEntity.DataListEntity d) {
        return d.getMyType();
    }

    @Override
    public void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, final MaterialRecommendEntity.BodyEntity.DataListEntity d, final int position) {
        switch (d.getMyType()) {
            case 0: {//超值特卖
                MessagePicturesLayoutScro lPictures = (MessagePicturesLayoutScro) holder.getView(R.id.l_pictures);

                HorizontalScrollView hs = (HorizontalScrollView) holder.getView(R.id.hs);
                DiscreteScrollView itemPicker = (DiscreteScrollView) holder.getView(R.id.item_picker);
                hs.setNestedScrollingEnabled(false);
                itemPicker.setNestedScrollingEnabled(false);
                lPictures.setNestedScrollingEnabled(false);
                final FoldTextView ftv = (FoldTextView) holder.getView(R.id.ftv);
                ftv.isExpand = false;

                if (d.getMaterialType() == 1) {//营销素材

                    hs.setVisibility(View.GONE);
                    itemPicker.setVisibility(View.VISIBLE);
                    itemPicker.setOrientation(DSVOrientation.HORIZONTAL);
                    LogUtil.jLog().e(new Gson().toJson(d));

                    final InfiniteScrollAdapter infiniteAdapter = InfiniteScrollAdapter.wrap(new ShopAdapter(d.getImageList(), new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String urlTag = (String) v.getTag();
                            int positionInDataSet = 0;
                           /* int position = (int) v.getTag(R.id.itemTag);
                            int positionInDataSet = infiniteAdapter.getRealPosition(position);*/

                            List<ImageView> imgs = new ArrayList<>();
                            imgs.add((ImageView) v);
                            List<String> urlList = new ArrayList<>();
                            for (int i = 0; i < d.getImageList().size(); i++) {
                                urlList.add(d.getImageList().get(i).getImageUrl());
                                if (d.getImageList().get(i).getImageUrl().equals(urlTag)) {
                                    positionInDataSet = i;
                                }
                            }
                            callback.onThumbPictureClick(positionInDataSet, (ImageView) v, imgs, urlList);

                        }
                    }));

                    final View svip = holder.getView(R.id.tv_share1);
                    svip.setOnClickListener(onClickListener);
                    svip.setTag(position);
                    svip.setTag(R.id.itemTag, d.getImageList().get(0).getNumber());
                    svip.setTag(R.id.itemTagDes, d.getImageList().get(0).getAdWord());

                    final View vip = holder.getView(R.id.tv_share0);
                    vip.setOnClickListener(onClickListener);
                    vip.setTag(position);
                    vip.setTag(R.id.itemTag, d.getImageList().get(0).getNumber());
                    vip.setTag(R.id.itemTagDes, d.getImageList().get(0).getAdWord());

                    final View tv_shareFenSi = holder.getView(R.id.tv_shareFenSi);
                    tv_shareFenSi.setOnClickListener(onClickListener);
                    tv_shareFenSi.setTag(position);
                    tv_shareFenSi.setTag(R.id.itemTag, d.getImageList().get(0).getNumber());
                    tv_shareFenSi.setTag(R.id.itemTagDes, d.getImageList().get(0).getAdWord());

                    holder.bindTextView(R.id.tv_Zhuan, "d.getHouse().getBrokerAgeValue()".contains("-") ? "立返￥" : "赚￥" + d.getHouse().getBrokerAgeValue());

                    itemPicker.addOnItemChangedListener(new DiscreteScrollView.OnItemChangedListener<RecyclerView.ViewHolder>() {
                        @Override
                        public void onCurrentItemChanged(@Nullable RecyclerView.ViewHolder viewHolder, int adapterPosition) {
                            int positionInDataSet = infiniteAdapter.getRealPosition(adapterPosition);
                            ftv.setText(d.getImageList().get(positionInDataSet).getAdWord());
                            svip.setTag(R.id.itemTag, positionInDataSet + "");
                            vip.setTag(R.id.itemTag, positionInDataSet + "");
                            tv_shareFenSi.setTag(R.id.itemTag, positionInDataSet + "");
                            svip.setTag(R.id.itemTagDes, d.getImageList().get(positionInDataSet).getAdWord());
                            vip.setTag(R.id.itemTagDes, d.getImageList().get(positionInDataSet).getAdWord());
                            tv_shareFenSi.setTag(R.id.itemTagDes, d.getImageList().get(positionInDataSet).getAdWord());

                        }
                    });

                    itemPicker.setAdapter(infiniteAdapter);
                    itemPicker.setItemTransitionTimeMillis(DiscreteScrollViewOptions.getTransitionTime());
                    itemPicker.setItemTransformer(new ScaleAndAlphaTransformer.Builder()
                            .setMinScale(0.8f)
                            .build());
                    infiniteAdapter.notifyDataSetChanged();

                    /*final RequestOptions options = new RequestOptions()
                            .error(R.drawable.ic_replace_0)
                            .fitCenter()
                            .placeholder(R.drawable.ic_replace_0);
                    if (d.getPicList() != null && d.getPicList().size() > 0) {

                        Glide.with(context).asBitmap().apply(options).load(d.getPicList().get(0)).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

                                int width = resource.getWidth();
                                int height = resource.getHeight();
                                if (width > PhoneUtil.dp2px(context, 180)) {
                                    img.getLayoutParams().height = height / width * PhoneUtil.dp2px(context, 180);
                                    img.getLayoutParams().width = PhoneUtil.dp2px(context, 180);
                                } else {
                                    img.getLayoutParams().width = width;

                                    img.getLayoutParams().height = height;
                                }
                                img.requestLayout();
                                img.setImageBitmap(resource);
                                img.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (mCallback != null) {
                                            List<ImageView> imgs = new ArrayList<>();
                                            imgs.add(img);

                                            List<String> url = new ArrayList<>();
                                            url.add(d.getPicList().get(0));
                                            mCallback.onThumbPictureClick((ImageView) v, imgs, url);
                                        }
                                    }
                                });
                           *//*
                            int imageWidth = resource.getWidth();
                            int imageHeight = resource.getHeight();
                            ViewGroup.LayoutParams para = img.getLayoutParams();
                            if (para.width > PhoneUtil.dp2px(context, 100)){
                                para.height = imageHeight/imageWidth*PhoneUtil.dp2px(context, 100);
                                para.width = PhoneUtil.dp2px(context, 100);
                            }else {
                                para.height = imageHeight;
                                para.width = imageWidth;
                            }

                            img.setLayoutParams(para);
                            Glide.with(context).asBitmap().apply(options).load(resource).into(img);*//*
                            }


                        });
                    } else {
                        Glide.with(context).asBitmap().apply(options).load(d.getPoster()).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
                            @Override
                            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

                                int width = resource.getWidth();
                                int height = resource.getHeight();
                                if (width > PhoneUtil.dp2px(context, 180)) {
                                    img.getLayoutParams().height = height / width * PhoneUtil.dp2px(context, 180);
                                    img.getLayoutParams().width = PhoneUtil.dp2px(context, 180);
                                } else {
                                    img.getLayoutParams().width = width;

                                    img.getLayoutParams().height = height;
                                }
                                img.requestLayout();
                                img.setImageBitmap(resource);
                                img.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        if (mCallback != null) {
                                            List<ImageView> imgs = new ArrayList<>();
                                            imgs.add(img);

                                            List<String> url = new ArrayList<>();
                                            url.add(d.getPoster());
                                            mCallback.onThumbPictureClick((ImageView) v, imgs, url);
                                        }
                                    }
                                });
                           *//*
                            int imageWidth = resource.getWidth();
                            int imageHeight = resource.getHeight();
                            ViewGroup.LayoutParams para = img.getLayoutParams();
                            if (para.width > PhoneUtil.dp2px(context, 100)){
                                para.height = imageHeight/imageWidth*PhoneUtil.dp2px(context, 100);
                                para.width = PhoneUtil.dp2px(context, 100);
                            }else {
                                para.height = imageHeight;
                                para.width = imageWidth;
                            }

                            img.setLayoutParams(para);
                            Glide.with(context).asBitmap().apply(options).load(resource).into(img);*//*
                            }


                        });
                    }*/


                } else {
                    itemPicker.setVisibility(View.GONE);
                    hs.setVisibility(View.VISIBLE);
                    ftv.setText(d.getAdWord());

                    lPictures.setCallback(mCallback);
                    lPictures.set(d.getPicList(), d.getPicList());
                    holder.bindTextView(R.id.tv_Zhuan, "d.getHouse().getBrokerAgeValue()".contains("-") ? "立返￥" : "赚￥" + d.getHouse().getBrokerAgeValue())
                           /* .setClickListenerAndTag(R.id.tv_shareFenSi, onClickListener, position)

                            .setClickListenerAndTag(R.id.tv_share0, onClickListener, position)
                            .setClickListenerAndTag(R.id.tv_share1, onClickListener, position)*/;
                    final View svip = holder.getView(R.id.tv_share1);
                    svip.setOnClickListener(onClickListener);
                    svip.setTag(position);
                    svip.setTag(R.id.itemTag, position+"");
                    svip.setTag(R.id.itemTagDes, d.getAdWord());

                    final View vip = holder.getView(R.id.tv_share0);
                    vip.setOnClickListener(onClickListener);
                    vip.setTag(position);
                    vip.setTag(R.id.itemTag, position+"");
                    vip.setTag(R.id.itemTagDes, d.getAdWord());

                    final View tv_shareFenSi = holder.getView(R.id.tv_shareFenSi);
                    tv_shareFenSi.setOnClickListener(onClickListener);
                    tv_shareFenSi.setTag(position);
                    tv_shareFenSi.setTag(R.id.itemTag, position+"");
                    tv_shareFenSi.setTag(R.id.itemTagDes, d.getAdWord());


                }


                View view = holder.getView(R.id.tv_Zhuan);
                if ("0".equals(d.getHouse().getBrokerAgeValue())) {
                    view.setVisibility(View.GONE);
                } else {
                    view.setVisibility(View.VISIBLE);
                }


                TextView svip = (TextView) holder.getView(R.id.tv_share1);
                TextView vip = (TextView) holder.getView(R.id.tv_share0);

                if ("2".equals(roleType)) {
                    if ("0".equals(roleInfo.getMember().getLevel())) {

                        svip.setVisibility(View.GONE);
                    } else {
                        svip.setVisibility(View.VISIBLE);
                    }
                } else {
                    svip.setVisibility(View.GONE);

                }

                /*RecyclerView inner = (RecyclerView) holder.getView(R.id.rv_inner);
                inner.setLayoutManager(new LinearLayoutManager(context, LinearLayout.HORIZONTAL, false));
                VipDynamicInnetAdapter vipDynamicInnetAdapter = new VipDynamicInnetAdapter(context, onClickListener);
                vipDynamicInnetAdapter.setFullScreen(true);
                inner.setAdapter(vipDynamicInnetAdapter);
                vipDynamicInnetAdapter.setData(d.getPicList());
                vipDynamicInnetAdapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(View v, int position) {
                       *//* if (mCallback != null) {
                            mCallback.onThumbPictureClick((ImageView) v, mVisiblePictureList, mDataList);
                        }*//*
                    }
                });
*/
            }
            break;

            case 1: {
                onAdapterCallbackListener.onCallback();
            }
            break;


        }
    }

    public interface Callback {
        void onThumbPictureClick(int positopn, ImageView i, List<ImageView> imageGroupList, List<String> urlList);
    }
}
